//--------------------------------------------------------------
//
// Qizhi Zhang 
// ezhung@gmail.com
// http://www.moorwind.com
//
//--------------------------------------------------------------
package reader.dao
{
	import __AS3__.vec.Vector;
	
	import flash.data.SQLConnection;
	import flash.data.SQLResult;
	
	import reader.model.vo.MediaVO;
	import reader.model.vo.UserVO;

	public class MediaDAO extends BaseDAO implements IMediaDAO
	{
		//////////////////////////////////////////////////////////////////////////////
		//SQLStatement queries
		
		private static const INSERT_MEDIA:String = "INSERT INTO media (type, name, ext, uri, date, time, platform, userId) VALUES(:type, :name, :ext, :uri, :date, :time, :platform, :userId)";
		private static const DELETE_MEDIA:String = "DELETE FROM media WHERE mid=:mid";
		private static const DELETE_MEDIA_BY_USER:String = "DELETE FROM media WHERE userId=:userId";
		private static const SELETE_MEDIA:String = "SELECT * FROM media WHERE mid=:mid";
		private static const SELETE_ALL_MEDIA:String = "SELECT * FROM media WHERE userId=:userId";
		private static const SELECT_COUNT:String = "SELECT COUNT(mid) FROM media WHERE userId=:userId";
		
		//////////////////////////////////////////////////////////////////////////////
		//constructor
		public function MediaDAO(connection:SQLConnection)
		{
			super(connection);
		}
		
		//////////////////////////////////////////////////////////////////////////////
		//methods
		
		//----------------------------------------------------------------------------
		//
		// INSERT
		//
		//----------------------------------------------------------------------------
		
		public function insertMedia(media:MediaVO, userid:int):int
		{
			var sqlResult:SQLResult = execute(INSERT_MEDIA, {type: media.type, name: media.name, ext: media.ext, uri: media.uri, date: media.date, time: media.date.getTime(), platform:media.platform, userId: userid});
            return int(sqlResult.lastInsertRowID);
		}
		
		//----------------------------------------------------------------------------
		//
		// DELETE
		//
		//----------------------------------------------------------------------------
		
		public function deleteMedia(media:MediaVO):Boolean
		{
            return drop(DELETE_MEDIA, {mid:media.mid});
		}
		
		public function deleteMediaByUser(user:UserVO):Boolean
		{
            return drop(DELETE_MEDIA_BY_USER, {userId: user.userId});
		}
		
		public function deleteMedias(medias:Vector.<MediaVO>):void
		{
			this.connection.begin();
            for each(var media:MediaVO in medias)
            {
            	executeTransaction(DELETE_MEDIA, {mid:media.mid});
            }
            
            try
            {
            	this.connection.commit();
            }
            catch(e:Error)
            {
            	trace(e);
            }
		}
		
		//----------------------------------------------------------------------------
		//
		// SELETE
		//
		//----------------------------------------------------------------------------
		
		public function selectMedia(mid:int):MediaVO
		{
			return select(SELETE_MEDIA, {mid: mid})[0];
		}
		
		public function selectAllMedia(userid:int, prefetch:int = -1):Vector.<MediaVO>
		{
			return select(SELETE_ALL_MEDIA, {userId: userid}, prefetch);
		}
		
		private function select(query:String, data:Object, prefetch:int = -1):Vector.<MediaVO>
		{
			var result:SQLResult = execute(query, data, prefetch);
			if(!result || !result.data)
			{
				return null;
			}
			
			var dataArray:Vector.<MediaVO> = new Vector.<MediaVO>();
			for each(var obj:Object in result.data)
			{
				var _data:MediaVO = new MediaVO();
				_data.mid = obj.mid;
				_data.date = obj.date;
				_data.name = obj.name;
				_data.ext = obj.ext;
				_data.type = obj.type;
				_data.uri = obj.uri;
				_data.platform = obj.platform;
				dataArray.push(_data);
			}
            return dataArray;
		}
		
		
		//----------------------------------------------------------------------------
		//
		// COUNT
		//
		//----------------------------------------------------------------------------
		
		public function getCount(user:UserVO):uint
		{
			return count(SELECT_COUNT, {userId: user.userId});
		}

	}
}